import BaseUtil from '@/BaseUtil';
import BuffUtil from '@/BuffUtil';
import RectUtil from '@/RectUtil';
import SelectUtil from '@/SelectUtil';
import { Rectangle } from '@/rect';
import BooleanCommon from 'code/Core/Common/BooleanCommon';
import BuffCommon from 'code/Core/Common/BuffCommon';
import MathCommon from 'code/Core/Common/MathCommon';
import MovementCommon from 'code/Core/Common/MovementCommon';
import UnitCommon from 'code/Core/Common/UnitCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/**
 * 陨石术
 **/
export default class FS_Meteorite extends AbilityBase {
    static instance: FS_Meteorite;
    buffType: AppBuffType = {
        id: 'buff_FS_Meteorite',
        art: this.art,
        name: this.abilityName,
        ubertip: this.ubertip,
        dur: 4,
    };
    constructor(id: string) {
        super(id);
        FS_Meteorite.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
    }
    init() {
        BuffUtil.registerBuffType(this.buffType);
    }
    onEnd(p: HitFlyConfig<Rectangle>) {
        KillUnit(p.target);
        p.other.destroy();
    }
    onInterval(p: HitFlyConfig<Rectangle>) {
        if ((p.curLoop - 1) % 20 == 0) {
            DestroyEffect(AddSpecialEffect('Objects\\Spawnmodels\\Other\\NeutralBuildingExplosion\\NeutralBuildingExplosion.mdl', p.curX, p.curY));
        }
        // 树木处理
        p.other.move(p.curX, p.curY);
        p.other.enumDestructables(null, this.canDestory);
        // 伤害
        let es = SelectUtil.getEnemyUnitsInRange(p.target, 175);
        let dmg = (30 + UnitCommon.取法强(p.caster) * 0.4) * this.getAbilityLevel(p.caster);
        DamageSystemInstance.applyAoeMagicDamage(es, p.caster, dmg, 'fire', this.id);
    }
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        let f = GetUnitFacing(source);
        let eff = CreateUnit(GetOwningPlayer(source), 'vyst', tarX, tarY, f);
        SetUnitColor(eff, PLAYER_COLOR_RED);
        BaseUtil.runLater(1, () => {
            KillUnit(eff);
            let ef = CreateUnit(GetOwningPlayer(source), 'vys1', tarX, tarY, f);
            SetUnitColor(ef, PLAYER_COLOR_RED);
            SetUnitTimeScalePercent(ef, 60);
            let r = RectUtil.GetRectFromCircle(tarX, tarY, 175);
            r.enumDestructables(null, this.canDestory);
            let buff = BuffCommon.addBuff({
                target: ef,
                caster: source,
                ablityId: this.id,
                buffTypeId: this.buffType.id,
            });
            MovementCommon.hitFly({
                target: ef,
                caster: source,
                distance: (buff.durMS / 1000) * 200,
                angle: f,
                time: buff.durMS / 1000,
                maxHeight: 0,
                onInterval: this.onInterval.bind(this),
                onEnd: this.onEnd.bind(this),
                other: r,
            });
        });
    }
    canDestory() {
        BooleanCommon.isTree(GetEnumDestructable()) && KillDestructable(GetEnumDestructable());
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
}
